//
//  WBvisitorView.swift
//  weibo
//
//  Created by swift on 2017/2/27.
//  Copyright © 2017年 jason. All rights reserved.
//

import UIKit

class WBvisitorView: UIView {
//访客视图信息字典[imagename/message]
    var visitorinfo:[String:String]?{
        didSet{
            //取出字典信息
            guard let imagename = visitorinfo?["imagename"],let message = visitorinfo?["message"] else {
                return
            }
            print(imagename)
            //设置消息
            tiplabel.text = message
            //设置图像
            if imagename == ""{
                startanimation()
                return
            }
            iconview.image = UIImage(named: imagename)
            houseview.isHidden = true
            maskview.isHidden = true
            
        }
    }
        //MARK:-构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupui()
       
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///旋转图标动画
    fileprivate func startanimation(){
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        animate.toValue = 2 * M_PI
        animate.repeatCount = MAXFLOAT
        animate.duration = 15
        animate.isRemovedOnCompletion = false
        iconview.layer.add(animate, forKey: nil)
    }
    //暴漏出来,让子类访问
     lazy var registerbutton:UIButton = UIButton.cz_textButton("注册", fontSize: 14, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
     lazy var logonbutton:UIButton = UIButton.cz_textButton("登陆", fontSize: 14, normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")

    fileprivate lazy var iconview:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    fileprivate lazy var maskview:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    fileprivate lazy var houseview:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    fileprivate lazy var tiplabel:UILabel = UILabel.cz_label(withText: "走走停停就会发现身边的美好    走走停停就会发现身边的美好", fontSize: 16, color: UIColor.darkGray)
    
    
}

//使用字典设置访客视图信息

extension WBvisitorView{
    fileprivate func setupui(){
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        //添加控件
        addSubview(iconview)
        addSubview(maskview)
        addSubview(houseview)
        addSubview(registerbutton)
        addSubview(tiplabel)
        addSubview(logonbutton)
        //文本居中
        tiplabel.textAlignment = .center
        //去掉自动布局
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        ///手动布局
        //1图像
        addConstraint(NSLayoutConstraint(item: iconview, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconview, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        //2小房子
        addConstraint(NSLayoutConstraint(item: houseview, attribute: .centerX, relatedBy: .equal, toItem: iconview, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseview, attribute: .centerY, relatedBy: .equal, toItem: iconview, attribute: .centerY, multiplier: 1.0, constant: 0))
        //3提示标签
        addConstraint(NSLayoutConstraint(item: tiplabel, attribute: .centerX, relatedBy: .equal, toItem: iconview, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tiplabel, attribute: .top, relatedBy: .equal, toItem: iconview, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: tiplabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 236))
        //4注册
        addConstraint(NSLayoutConstraint(item: registerbutton, attribute: .left, relatedBy: .equal, toItem: tiplabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerbutton, attribute: .top, relatedBy: .equal, toItem: tiplabel, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerbutton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        //登录
        addConstraint(NSLayoutConstraint(item: logonbutton, attribute: .right, relatedBy: .equal, toItem: tiplabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: logonbutton, attribute: .top, relatedBy: .equal, toItem: tiplabel, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: logonbutton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        //mask布局vfl
        let viewdict:[String:Any] = ["maskview":maskview,"registerbutton":registerbutton]
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskview]-0-|", options: [], metrics: nil, views: viewdict))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskview]-(-20)-[registerbutton]", options: [], metrics: nil, views: viewdict))

           }
}
